#This is an ELIZA program.  It is designed to be a school advisor for a college
#student.  You can type in text to it and it will respond to you as a school
#advisor might respond. The algorithm works in this way. First it checks to see
#if you wrote "quit" and if you did it will exit the program.  Next it converts
#the pronouns of the user's statement into the form it will respond in for
#example I to you.  Next it looks for a keyword in the sentence and returns the
#keyword's precendence.  After that it takes the precedence and according to the
#precedence level it will check the form of the input and give back a responce 
#key.  Next if the response key is zero which means that it couldn't find a 
#keyword match. If this happens three times it will try to get you to talk about
#something it knows.  Next it will output a response based on the response key
#generated. ELIZA expects that you will put a period at the end
#of each sentence



#initializes all of the stored responses and bank of keywords
initialize();

#Greeting Line
print "ELIZA~>Hello, I am ELIZA your college advisor. How can I help you?\n";

#main loop
while(<>){#each iteration counts for a turn in the conversation

    exit_eliza();#decides whether to exit or not

    pronoun_conversion();#converts pronouns

    choose_keyword();#chooses the most important keyword

    get_response_key($chosen_keyword);#gets the entry for the response list for this keyword

    loop_prevent($resp_key);#prevents the program from getting stuck in a loop

    output_response($resp_key);#outputs a response

    clean();#cleans out variables 
    
}


################################################################################
#This function checks for the word "exit" and will quit the program if it is

sub exit_eliza{

    if(/^exit$/){
	exit;
    }

}


#################################################################################Initializes the dictionary for the keywords and the bank of responses##########

sub initialize{

    $tell_me_more = 0;#number of times the program has given a "tell me more" rsponse in a row

    %keywords = ("concerned", 1, "angry", 1, "mad", 1, "annoyed", 1, "worried", 1, "sad", 1, "failed", 2, "didn't pass", 2, "did not pass", 2, "if", 3, "study", 4, "studied", 4, "class", 5, "are", 6, "classes", 7, "want", 8, "like", 9, "credits", 10, "major", 11, "graduation", 12, "school", 13, "teachers", 14, "teacher", 14, "professor", 14, "professors", 14, "job", 15, "jobs", 15, "career", 15, "careers", 15, "resume", 16);

    #%response_key = ("concerned", 1,  "failed", 2); 

    @response1 = ("Why are you $chosen_keyword that $first");

    @responses = (@response1);

    
}

################################################################################
#This subroutine changes the pronouns to the form used in the response##########

sub pronoun_conversion{

    s/\bI am\b/I are1/gi;#1's are used in order to mark pronouns so they will
    s/(\bam|\bAm) I\b/are1 I/g;#not be converted back to their original form
    s/\bI\b/you1/gi;
    s/\byou are\b/you am/g;
    s/\bare you\b/am you/g;
    s/\byou\b/I/g;
    s/\byou1'm\b/you're1/g;#note that you gets marked perviously
    s/\bI're\b/I'm/g;
    s/\bmy\b/your1/g;#take the marks off of the words
    s/\byour\b/my/g;
    s/are1/are/g;
    s/you1/you/g;
    s/you're1/you're/g;
    s/your1/your/g;
    s/\bme\b/you/g;
}

################################################################################
#Scans the string for the keyword with highest precedence and stores it in######
#$chosen_keyword################################################################
sub choose_keyword{
    
    $chosen_keyword = "";#the keyword that will be chosen

    $highest_precedence = 0;#the value of this is the keyword with the highest
    #precedence currently

    while(($key, $value) = each(%keywords)){#iterate through the keyword hash
	
	if(/$key/i){#check if keyword is in the string
	    
	    if($value > $highest_precedence){#check precedence level
		
		$chosen_keyword = $key;#set current key as the chosen keyword

		$highest_precedence = $value;#reset precedence

	    }#end if($value...

	}#end if(/$key/)...
    
    }#end while(($key...

}

################################################################################
#takes in a keyword and gives back its location in the response array###########
#returns key in the variable $resp_key

sub get_response_key{

    if($highest_precedence == 0){#if no keyword is found
	    $resp_key = 0;

    }
    elsif($highest_precedence == 1){#if the keyword such as concerned is found

	if(/you are $_[0] that (.*)./){
	    $resp_key = 1;
	    $first = $1;
	}

    }
    elsif($highest_precedence == 2){#if keyword about failing is found

	if(/you $_[0] (.*)./){
	    $resp_key = 2;
	    $first = $1;
	}

    }
    elsif($highest_precedence == 3){#if the keyword "if" is found

	if(/$_[0] (.*)./){
	    $resp_key = 3;
	    $first = $1;
	}

    }
     elsif($highest_precedence == 4){#if the keyword "study" is found

	if(/(did not|didn't|don't|do not) study (.*)./){
	    $resp_key = 4;
	    $first = " ".$1;
	    $second = $2;
	}
	elsif(/$chosen_keyword (.*)./){
	    $resp_key = 4;
	    $first = "";
	    $second = $1;
	}

    }

     elsif($highest_precedence == 5){#if the keyword class is found

	if(/$chosen_keyword is (.*)./){
	    $resp_key = 5;
	    $first = $1;
	}
	else{
	    $resp_key = 6;
	}

    }

     elsif($highest_precedence == 6){#if the keyword are is found

	if(/you $chosen_keyword (.*)./){
	    $resp_key = 7;
	    $first = $1;
	}

    }
    
     elsif($highest_precedence == 7){#if the keyword class is found

	if(/$chosen_keyword are (.*)./i){
	    $resp_key = 8;
	    $first = " your";
	    $second = $1;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 9;
	}

    }

     elsif($highest_precedence == 8){#if the keyword want is found

	if(/you (don't |do not )?$chosen_keyword (.*)./i){
	    $resp_key = 10;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 11;
	}

    }

     elsif($highest_precedence == 9){#if the keyword like is found

	if(/you (don't |do not )?$chosen_keyword (.*)./i){
	    $resp_key = 10;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 11;
	}

    }

     elsif($highest_precedence == 10){#if the keyword credits is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 13;
	    $first = "your";
	}

    }

     elsif($highest_precedence == 11){#if the keyword major is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 13;
	    $first = "your";
	}

    }

     elsif($highest_precedence == 12){#if the keyword graduation is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 13;
	    $first = "";
	}

    }

    elsif($highest_precedence == 13){#if the keyword school is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 13;
	    $first = "";
	}

    }

     elsif($highest_precedence == 14){#if the keyword teacher or professor is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 13;
	    $first = "your";
	}

    }

     elsif($highest_precedence == 15){#if the keyword job or career is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 14;
	}

    }

     elsif($highest_precedence == 16){#if the keyword resume is found

	if(/$chosen_keyword (is|are) (.*)./i){
	    $resp_key = 12;
	    $first = $1;
	    $second = $2;
	}
	elsif(/$chosen_keyword/){
	    $resp_key = 15;
	}

    }

}

################################################################################
#prevents the program from getting caught in a "tell me more" loop
#param 1: the resp_key of the response

sub loop_prevent{

    if($_[0] == 0){#if there were no matches 
	$tell_me_more += 1;
    }else{
	$tell_me_more = 0;
    }
    if($tell_me_more == 3){
	$resp_key = 16;
    }

}


################################################################################
#prints out a response
#param 1: the resp_key of the response

sub output_response{

    print "ELIZA~>";

     if($_[0] == 0){#reponses for no keywords found
	$res = int(rand(3));
	
	
	    if($res == 0) { print "Tell me more about that.\n"; }
	    elsif($res == 1) { print "Could you explain that more?\n"; }
	    elsif($res == 2) { print "Could you explain further about that?\n"; }
	    elsif($res == 3) { print "Let's talk more about that.\n"; }
	
    }

    elsif($_[0] == 1){#responses for "you are concerned..."
	$res = int(rand(3));
	
	    if($res == 0) { print "Why are you ".$chosen_keyword." that ".$first."?\n"; }
	    elsif($res == 1) { print "Is there a reason you are ".$chosen_keyword." that ".$first."?\n"; }
	    elsif($res == 2) { print "Tell me more about why you are ".$chosen_keyword.".\n"; }
	    elsif($res == 3) { print "Have you ever been ".$chosen_keyword." before?\n"; }
	
    }

    elsif($_[0] == 2){#responses for "I failed..."
	$res = int(rand(3));
	

	    if($res == 0) { print "How come you ".$chosen_keyword." ".$first."?\n"; }
	    elsif($res == 1) { print "Is there a reason you ".$chosen_keyword." ".$first."?\n"; }
	    elsif($res == 2) { print "Did you study enough for ".$first.".\n"; }
	    elsif($res == 3) { print "Is this a common thing?\n"; }
	
    }

    elsif($_[0] == 3){#responses for "...if..."
	$res = int(rand(3));
	
	    if($res == 0) { print "Does it seem likely ".$first."?\n"; }
	    elsif($res == 1) { print "Do you think ".$first."?\n"; }
	    elsif($res == 2) { print "Does it seem possible?"; }
	    elsif($res == 3) { print "You seem uncertain\n."; }

    }

      elsif($_[0] == 4){#responses for "...study..."
	$res = int(rand(3));
	
	    if($res == 0) { print "How come you".$first." ".$chosen_keyword." ".$second."?\n"; }
	    elsif($res == 1) { print "Why do you think you".$first." ".$chosen_keyword." ".$second."?\n"; }
	    elsif($res == 2) { print "Tell me about the classes you study for?\n"; }
	    elsif($res == 3) { print "Do you think you study more?\n."; }
	
    }

     elsif($_[0] == 5){#responses for "class is..."
	$res = int(rand(3));
	
	    if($res == 0) { print "How come ".$chosen_keyword." is ".$first."?\n"; }
	    elsif($res == 1) { print "Why do you think you ".$chosen_keyword." is ".$first."?\n"; }
	    elsif($res == 2) { print "Are you satisfied with this?\n"; }
	    elsif($res == 3) { print "Is there anything in class that concerns you?\n."; }

    }

     elsif($_[0] == 6){#responses for "class"
	$res = int(rand(3));
	
	    if($res == 0) { print "Could you tell me more about your class.\n"; }
	    elsif($res == 1) { print "Let's talk more specific about your class\n"; }
	    elsif($res == 2) { print "Tell me about the classes you like.\n"; }
	    elsif($res == 3) { print "Let's talk more about class?.\n"; }
	
    }

      elsif($_[0] == 7){#responses for "I am..."
	$res = int(rand(3));
	
	    if($res == 0) { print "Why do you think you are ".$first."?\n"; }
	    elsif($res == 1) { print "Let's consider why you think you are ".$first.".\n"; }
	    elsif($res == 2) { print "Tell me about why you are ".$first.".\n"; }
	    elsif($res == 3) { print "You are ".$first."?\n."; }
	
    }

     elsif($_[0] == 8){#responses for "classes are"
	$res = int(rand(3));
	
	    if($res == 0) { print "Why do you think".$first." ".$chosen_keyword." are ".$second."?\n"; }
	    elsif($res == 1) { print "Let's consider why you think".$first." ".$chosen_keyword." are ".$second.".\n"; }
	    elsif($res == 2) { print "Tell me about why".$first." ".$chosen_keyword." are ".$first.".\n"; }
	    elsif($res == 3) { print $first." ".$chosen_keyword." are ".$second."?\n."; }
	
    }

      elsif($_[0] == 9){#responses for "classes"
	$res = int(rand(3));
	
	    if($res == 0) { print "Tell me more about your classes.\n"; }
	    elsif($res == 1) { print "Could you explain further about your classes.\n"; }
	    elsif($res == 2) { print "Let's talk about your classes.\n"; }
	    elsif($res == 3) { print "Could you explain more about your classes?\n."; }
	
    }

       elsif($_[0] == 10){#responses for "I want|like..."
	$res = int(rand(3));
	
	    if($res == 0) { print "How come you ".$first.$chosen_keyword." ".$second."?\n"; }
	    elsif($res == 1) { print "Let's consider why you ".$first.$chosen_keyword." ".$second.".\n"; }
	    elsif($res == 2) { print "Tell me why you ".$first.$chosen_keyword." ".$second.".\n"; }
	    elsif($res == 3) { print "You ".$first." ".$chosen_keyword.$second."?\n."; }
	
    }

       elsif($_[0] == 11){#responses for "want|like"
	$res = int(rand(3));
	
	    if($res == 0) { print "Tell me more about what you ".$chosen_keyword.".\n"; }
	    elsif($res == 1) { print "Could you explain further about what you ".$chosen_keyword.".\n"; }
	    elsif($res == 2) { print "Let's talk about what you ".$chosen_keyword.".\n"; }
	    elsif($res == 3) { print "Could you explain more about what you ".$chosen_keyword."?\n."; }
	
    }

       elsif($_[0] == 12){#responses for "credits|major|graduation... is..."
	$res = int(rand(3));
	
	    if($res == 0) { print "Why do you think ".$chosen_keyword." ".$first." ".$second."?\n"; }
	    elsif($res == 1) { print "Let's consider why you think ".$chosen_keyword." ".$first." ".$second.".\n"; }
	    elsif($res == 2) { print "Tell me why you think ".$chosen_keyword." ".$first." ".$second.".\n"; }
	    elsif($res == 3) { print "You think ".$chosen_keyword." ".$first." ".$second."?\n."; }
	
    }

       elsif($_[0] == 13){#responses for "credits|major|graduation..."
	$res = int(rand(4));
	
	    if($res == 0) { print "Tell me more about ".$first." ".$chosen_keyword.".\n"; }
	    elsif($res == 1) { print "Could you explain further about ".$first." ".$chosen_keyword.".\n"; }
	    elsif($res == 2) { print "Let's talk about ".$first." ".$chosen_keyword."\n"; }
	    elsif($res == 3) { print "Could you explain more about ".$first." ".$chosen_keyword."?\n"; }
	    elsif($res == 4) { print "Enough about ".$first." ".$chosen_keyword." let's talk about your classes.\n"; }
	
    }

       elsif($_[0] == 14){#response for "career"
	$res = int(rand(3));
	
	
	    if($res == 0) { print "Tell me more about what you wanna do after college.\n"; }
	    elsif($res == 1) { print "Could you explain further about where you want to work after school.\n"; }
	    elsif($res == 2) { print "Let's talk about your options after school.\n"; }
	    elsif($res == 3) { print "Perhaps we should talk about graduation?\n."; }
      
    }

       elsif($_[0] == 15){#response for "resume"
	$res = int(rand(3));
	
	    if($res == 0) { print "Tell me more about your resume.\n"; }
	    elsif($res == 1) { print "Could explain more about your resume.\n"; }
	    elsif($res == 2) { print "Let's talk about your resume.\n"; }
	    elsif($res == 3) { print "Could you explain more about your resume?\n."; }
	
    }

      elsif($_[0] == 16){#interjections if we get into a tell me more loop
	$res = int(rand(3));
	
	    if($res == 0) { print "Why don't we talk about your major?\n"; }
	    elsif($res == 1) { print "Let's talk about your classes.\n"; }
	    elsif($res == 2) { print "Tell me about your professors.\n"; }
	    elsif($res == 3) { print "Could we talk about your resume?\n"; }
	
    }


}


sub clean{

    $chosen_keyword = "";
    $resp_key = 0;
    $highest_precedence = 0;

}
